package com.wtwd.campus.vcom.controller;

import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.utils.MD5Utils;
import com.wtwd.campus.vcom.common.RespCode;
import com.wtwd.campus.vcom.common.RespEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/1/4 15:03
 * @Description:
 * 考虑到后续拓展的情况比较少，因此这里写死对应的appKey和appSecret
 * 设备表分配deviceType和customerId
 */
@Api(tags = "鉴权")
@RestController
@RequestMapping("/vcom/identity/")
public class IdentityController {

    private static final String appKey = "rgGCXyczfUymbtLS";
    private static final String appSecret = "ZzAsydyTpM4GMyMu";
    private static final int vcomCustomerId = 1;

    @Autowired
    RedisService redisService;

    /**
     * 威科姆客户获取token
     * 有效时间 24小时，重新获取则覆盖
     * @param appKey
     * @param appSecret
     * @return
     */
    @ApiOperation(value = "获取校验token", notes = "")
    @GetMapping("/getAccessToken")
    public RespEntity getAccessToken(@RequestParam("appKey") String appKey, @RequestParam("appSecret") String appSecret){
        // 校验appKey是否存在
        if(!IdentityController.appKey.equals(appKey)){
            return new RespEntity(RespCode.APP_KEY_NOT_FOUND);
        }
        // 校验appSecret是否正确
        String md5Secret = MD5Utils.encode(IdentityController.appSecret);
        if (!appSecret.equalsIgnoreCase(md5Secret)) {
            return new RespEntity(RespCode.APP_SECRET_ERROR);
        }
        // 创建token
        String token = redisService.createVcomTypeToken(IdentityController.vcomCustomerId);
        return new RespEntity(RespCode.SUCCESS,token);
    }

}
